package library_management.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class BookList {
    private static List<Book> books = new ArrayList<>();
    private static List<String> booksName = new ArrayList<>();
    static {
        books.add(new Book("水浒传","施耐庵",36.5,"四大名著"));
        books.add(new Book("三国演义","罗贯中",43.7,"四大名著"));
        books.add(new Book("西游记","吴承恩",68.9,"四大名著"));
        books.add(new Book("红楼梦","曹雪芹",35.1,"四大名著"));
        books.add(new Book("格林童话","格林兄弟",55.3,"故事书"));
        books.add(new Book("五年高考三年模拟","曲一线",88.8,"工具书"));

        booksName.add("水浒传");
        booksName.add("三国演义");
        booksName.add("西游记");
        booksName.add("红楼梦");
        booksName.add("格林童话");
        booksName.add("五年高考三年模拟");
    }

    //判断是否存在newBook这本书
    public boolean isContains(String newBook){
        return booksName.contains(newBook);
    }

    //判断书有没有借出
    public boolean isBookBorrow(String bookName){
        Iterator<Book> iterable=books.iterator();
        while ((iterable.hasNext()))
        {
            Book book = iterable.next();
            if(book.getName().equals(bookName))
            {
                return book.isBorrowed();
            }
        }
        return false;
    }

    //添加书籍
    public void addNewBook(Book book){
        books.add(book);
        booksName.add(book.getName());
    }

    //遍历书籍列表
    public void printBooks(){
        Iterator<Book> iterable=books.iterator();
        while (iterable.hasNext())
        {
            Book book=iterable.next();
            System.out.println(book);
        }
    }

    //借书
    public void borrow(String borrowName){
        Iterator<Book> iterable=books.iterator();
        int i=0;
        Book borrowBook=books.get(i);
        while (iterable.hasNext())
        {
            borrowBook=iterable.next();
            if(borrowBook.getName().equals(borrowName))
            {
                borrowBook= books.get(i);
                borrowBook.setBorrowed(true);
                break;
            }
            i++;
        }
    }

    //删除
    public void remove(String removeName){
        Iterator<Book> iterable=books.iterator();
        int i=0;
        while (iterable.hasNext()){
            Book book=iterable.next();
            if(book.getName().equals(removeName))
            {
                books.remove(i);
                break;
            }
            i++;
        }
    }

    //还书
    public void returnBook(String returnName){
        Iterator<Book> iterable=books.iterator();
        int i=0;
        Book returnBook=books.get(i);
        while (iterable.hasNext())
        {
            returnBook=iterable.next();
            if(returnBook.getName().equals(returnName))
            {
                returnBook= books.get(i);
                returnBook.setBorrowed(false);
                break;
            }
            i++;
        }
    }

    //更新书籍
    public void update(String updateName){
        Iterator<Book> iterator= books.iterator();
        int i=0;
        Book updateBook=books.get(0);
        Scanner scanner=new Scanner(System.in);
        while (iterator.hasNext())
        {
            updateBook= iterator.next();
            if(updateBook.getName().equals(updateName))
            {
                System.out.println("您是否想修改价格？若想则输入1，若不想则输入2");
                int choice=scanner.nextInt();
                if(choice==1)
                {
                    System.out.println("请输入您想修改的价格：");
                    double newPrice= scanner.nextDouble();
                    updateBook.setPrice(newPrice);
                }
                System.out.println("您是否想修改书目类别？若想则输入1，若不想则输入2");
                int choose=scanner.nextInt();
                if(choose==1)
                {
                    System.out.println("请输入您想修改的类别：");
                    String newType= scanner.next();
                    updateBook.setType(newType);
                }
            }
            i++;
        }
    }
}

